'''
Created on 2010/10/31

@author: yukiyonezawa
'''
from pyrfid.ApplicationInterface.ApplicationCommand import ApplicationCommand
from pyrfid.asn1.Encoder import Encoder

class ConfigureAFICommand(ApplicationCommand):
    
    def __init__(self, tag_id, afi, afi_lock):
        print "ConfigureAFICommand"
        self.COMMAND_MODULE_OID = '28FC597E01'
        self.RESPONSE_MODULE_OID = '28FC597F01'
        self.TAG_ID = tag_id
        self.AFI = afi
        self.AFI_LOCK = afi_lock
        self.listCmdAndData = []
        self.ENCODED_COMMAND = ''
    
    
    def encode(self):
        """docstring for encode"""
        
        encoder = Encoder()
        
        addObjectLength = 0
        
        self.listCmdAndData.append(['06', '05', self.COMMAND_MODULE_OID])
        self.listCmdAndData.append(['30', '', ''])
        
        #tagID
        lenTagIDHex = encoder.lengthEncode(len(self.TAG_ID) / 2)
        self.listCmdAndData.append([ApplicationCommand.OCTET_STRING, lenTagIDHex, self.TAG_ID])
        
        #AFI(Application Family ID)
        self.listCmdAndData.append([ApplicationCommand.INTEGER, '01', encoder.intEncode(self.AFI.APPLICATION_FAMILY)])
        self.listCmdAndData.append([ApplicationCommand.INTEGER, '01', encoder.intEncode(self.AFI.APPLICATION_SUB_FAMILY)])
        
        #AFI LOCK
        self.listCmdAndData.append([ApplicationCommand.BOOLEAN, '01', encoder.boolEncode(self.AFI_LOCK)])
        
        self.listCmdAndData[1][1] = str(encoder.lengthEncode(int(lenTagIDHex, 16) + 1 + 1 + 1))

        self._convertToHexString()
        return self.ENCODED_COMMAND
